using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;

using Danser_Entity.Queries.Data;
using Danser_Entity.Queries.Filters;

namespace Danser_Entity.Utils
{
    /// <summary>
    /// Supplies utilities for the use of the other Danser components
    /// </summary>
    public class Utils
    {
        /// <summary>
        /// Check for valid mailAddress list.
        /// </summary>
        /// <param name="mailList"></param>
        /// <returns></returns>
        public static bool isMailListValid(String mailList)
        {
            String[] mailAddresses = mailList.Split(new char[] { '\n', '\r', ';' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < mailAddresses.Length; i++)
            {
                if (!isMailAddressValid(mailAddresses[i]))
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Check for valid mailAddress using the .NET capabilities
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        private static bool isMailAddressValid(String address)
        {
            try
            {
                System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(address);
                return true;
            }
            catch //everything
            {
                return false;
            }
        }
 
        /// <summary>
        /// Aggregate all list elements into 1 string with the specified separator 
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static String ListItemsToString(IList<String> elements, char separator)
        {
            StringBuilder listAsString = new StringBuilder();
            for (int i = 0 ; i < elements.Count; i++)
            {
                listAsString.Append(elements[i] + separator);
            }
            return listAsString.ToString();
        }

        /// <summary>
        /// Aggregate all elements to 1 string with the specified separator
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static String MergeArrayToString(String[] elements, char separator)
        {
            IList<String> toConvert = new List<String>(elements);
            
            return ListItemsToString(toConvert, separator);
        }

        /// <summary>
        /// Create a List of strings out of the given string as parameter
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static String[] StringToArray(String toConvert, char separator)
        {
            String[] array = null;

            if (toConvert != null)
                array = toConvert.Split(
                    new char[] { separator }, 
                    StringSplitOptions.RemoveEmptyEntries);
            
            return array;
        }

        /// <summary>
        /// convert string to a list of items
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static IList<String> StringTolist(String toConvert, char separator)
        {
            String[] array = StringToArray(toConvert, separator);
            if (array == null)
                return null;

            IList<String> stringList = new List<String>(array);

            return stringList;
        }

        /// <summary>
        /// Check if the DB field is of type string
        /// </summary>
        /// <param name="field">the name of the DB field</param>
        /// <returns></returns>
        public static bool IsFieldTypeString(String field)
        {
            Regex stringFields = new Regex(DBConstants.STRING_TYPED_FIEDLS);

            return stringFields.IsMatch(field);
        }

        /// <summary>
        /// Check if the DB field is of type boolean
        /// </summary>
        /// <param name="field">the name of the DB field</param>
        /// <returns></returns>
        public static bool IsFieldTypBoolean(String field)
        {
            Regex boolFields = new Regex(DBConstants.BOOLEAN_TYPED_FIELDS);

            return boolFields.IsMatch(field);
        }

        /// <summary>
        /// Check if given operator name matches the given pattern
        /// </summary>
        /// <param name="pattern">String must be in regular expression format</param>
        /// <param name="cOperator">the operator name to check</param>
        /// <returns>true if there's a match, false otherwise</returns>
        public static bool IsOperatorMatch(String pattern, String cOperator)
        {
            Regex matchOperators = new Regex(pattern);

            return matchOperators.IsMatch(cOperator);
        }

        /// <summary>
        /// Convert a conditions string to its equivalent SQL conditional string
        /// </summary>
        /// <param name="simpleString">String to convert</param>
        /// <returns>Equivalent String in SQL syntax</returns>
        public static String String2SQLExpression(String simpleString)
        {
            RuleExpression parsedRule = RuleExpression.ParseRuleFromString(simpleString);
            
            String SQLString = parsedRule.ConditionsToString(true);
            
            return SQLString;
        }
        
    }
}
